//
//  QAZoneTableViewCell.m
//  CYWFrame
//
//  Created by Macx on 2017/9/26.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "QAZoneTableViewCell.h"
#import "Question.h"

#import "LZPictureBrowser.h"
#import "PushPerHome.h"

@implementation QAZoneTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.nickName.font = kUHSystemFontWithSize(14);
    self.title.font = kUHSystemFontWithSize(14);
    self.date.font = kUHSystemFontWithSize(12);
    self.viewNum.font = kUHSystemFontWithSize(13);
    self.commentNum.font = kUHSystemFontWithSize(13);
    
    CGSize size2 = [LZCTools textSizeWithText:@"5小时之前" font:kUHSystemFontWithSize(12)];
    
    self.headPic.frame = CGRectMake(10, 10, SW(9), SW(9));
    self.headPic.layer.cornerRadius = SW(18);
    self.headPic.layer.masksToBounds = YES;
    
    self.role.frame = CGRectMake(self.headPic.x+self.headPic.width/3*2, self.headPic.y+self.headPic.height/3*2, self.headPic.width/3, self.headPic.width/3);
    
    self.date.frame = CGRectMake(self.headPic.x+self.headPic.width+10, self.headPic.y+self.headPic.height-size2.height, size2.width, size2.height);

    
    self.money.titleLabel.font = kUHSystemFontWithSize(13);
    self.money.userInteractionEnabled = NO;
    [self.money setTitleColor:kWhiteColor forState:UIControlStateNormal];
    
    
    
    int state = [LZCTools thinkEnv];
    if (state == 0) {
        self.money.hidden = YES;
    }
    
    
    self.deleteBtn.titleLabel.font = kUHSystemFontWithSize(12);
    self.deleteBtn.frame = CGRectMake(SCREEN_WIDTH-self.headPic.width, 10, self.headPic.width, self.headPic.height);
    self.deleteBtn.hidden = YES;
    
    [self.praise setImage:[IMAGE(@"index_zan") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self.praise setTitle:@" 110" forState:UIControlStateNormal];
    self.praise.titleLabel.font = kUHSystemFontWithSize(13);
    
    [self.disPraise setImage:[IMAGE(@"question_cai") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self.disPraise setTitle:@" 110" forState:UIControlStateNormal];
    self.disPraise.titleLabel.font = kUHSystemFontWithSize(13);
    
    self.praise.hidden = YES;
    self.disPraise.hidden = YES;
    
    [self.qaBtn setImage:[IMAGE(@"icon_qna_write") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self.qaBtn setTitle:@" 写回答" forState:UIControlStateNormal];
    self.qaBtn.titleLabel.font = kUHSystemFontWithSize(13);
    [self.qaBtn setTitleColor:RGB(69, 116, 159) forState:UIControlStateNormal];
    
    self.line.backgroundColor = UISeparatedColorRGB;
    
    
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    for (int i = 0; i < 3; i++) {
        UIImageView *img = [[UIImageView alloc] init];
        [self addSubview:img];
        img.hidden = YES;
        [arr addObject:img];
    }
    self.imgArr = [NSArray arrayWithArray:arr];
    
}

- (void)setModel:(Question *)model {
    
    if (_model != model) {
        _model = model;
        
        [self.headPic sd_setImageWithURL:[NSURL URLWithString:[model.author valueForKey:@"face"]] placeholderImage:IMAGE(@"placeHolder")];
        
        [self.headPic addActionWithblock:^{
            
            [PushPerHome pushPerHomeWithUserInfo:[model valueForKey:@"author"]];
        }];
        
        
        self.nickName.text = [model.author valueForKey:@"nickname"];
        if ([[model.author valueForKey:@"sex"] intValue] == 1) {
            self.gender.image = IMAGE(@"sex_boy");
        }
        else
            self.gender.image = IMAGE(@"sex_girl");
        
        self.date.text = model.human_time;
        
        if ([[model.author valueForKey:@"role"] isEqualToString:@"1"]) {
            self.role.image = IMAGE(@"");
        }
        else if ([[model.author valueForKey:@"role"] isEqualToString:@"2"]) {
            self.role.image = IMAGE(@"icon_add_v");
        }
        else if ([[model.author valueForKey:@"role"] isEqualToString:@"3"]) {
            self.role.image = IMAGE(@"icon_add_v_expert");
        }
       
        
        if (model.title) {
            self.title.text = model.title;
        }
        else {
            self.title.text = model.body;
            
            self.title.textColor = kGrayColor;
            
            if ([model.is_paid isEqualToString:@"N"] && [model.type isEqualToString:@"charge"]) {
                
                self.title.text = [NSString stringWithFormat:@"%@[%.2lf元]", model.body, model.amount/100];
                self.title.textColor = UIMainColorRGB;
                
                NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:self.title.text];
                
                NSRange range1=[[noteStr string]rangeOfString:model.body];
                [noteStr addAttribute:NSForegroundColorAttributeName value:kGrayColor range:range1];
                [self.title setAttributedText:noteStr] ;
            }
            
            
            int state = [LZCTools thinkEnv];
            if (state == 0) {
                self.title.text = @"哈哈";
                self.title.textColor = kGrayColor;
            }
            
        }
        
        
        if (model.reward == 0) {
            [self.money setTitle:@"未悬赏" forState:UIControlStateNormal];
        }
        else {
            [self.money setTitle:[NSString stringWithFormat:@"悬赏%.2f元", [[NSString stringWithFormat:@"%ld", model.reward] floatValue]/100] forState:UIControlStateNormal];
        }
        
        
        
        
        self.viewNum.text = [NSString stringWithFormat:@"%ld浏览", model.views_count];
        if (model.answers_count) {
            self.commentNum.text = [NSString stringWithFormat:@"%ld评论", model.answers_count];
        }
        else {
            self.commentNum.text = [NSString stringWithFormat:@"%ld评论", model.comments_count];
        }

        
        // 布局
        CGSize size1 = [LZCTools textSizeWithText:self.nickName.text font:kUHSystemFontWithSize(14)];
        
        CGSize titleSize = [self.title.text sizeWithFont:kUHSystemFontWithSize(14) constrainedToSize:CGSizeMake(SCREEN_WIDTH-20, size1.height*10) lineBreakMode:NSLineBreakByWordWrapping];
        CGSize size2 = [LZCTools textSizeWithText:@"999999评论" font:self.commentNum.font];
        
        self.nickName.frame = CGRectMake(self.headPic.x+self.headPic.width+10, 10, size1.width, size1.height);
        self.gender.frame = CGRectMake(self.nickName.x+self.nickName.width+5, 12, size1.height-4, size1.height-4);
        self.title.frame = CGRectMake(10, self.headPic.y+self.headPic.height+10, SCREEN_WIDTH-20, titleSize.height);
        
        CGFloat imgW = (SCREEN_WIDTH-40)/3;
        CGFloat imgY = self.title.y+self.title.height+10;
        CGFloat bottomY = self.title.y+self.title.height+10;
        if (model.pics.count > 0) {
            
            // 取屏
            self.mArr = [[NSMutableArray alloc] init];
            [self.mArr addObjectsFromArray:model.pics];
            NSArray *arr = model.pics;
            
            for (int i = 0; i < arr.count; i++) {
                
                UIImageView *imageView = self.imgArr[i];
                imageView.hidden = NO;
                imageView.contentMode = UIViewContentModeScaleAspectFill;
                imageView.clipsToBounds = YES;
                [imageView addTarget:self action:@selector(imageClick:)];
                [imageView sd_setImageWithURL:[NSURL URLWithString:arr[i]] placeholderImage:IMAGE(@"placeHolder")];
                imageView.tag = 1+i;
                
                imageView.frame = CGRectMake(self.title.x+(imgW+10)*i,imgY, imgW, imgW);
            }
            
            bottomY = imgY+imgW+10;
        }
        else {
            for (UIImageView *imgView in self.imgArr) {
                imgView.image = [UIImage imageNamed:@""];
                imgView.hidden = YES;
            }
        }

        
        self.viewNum.frame = CGRectMake(10, bottomY, size2.width, size2.height);
        self.commentNum.frame = CGRectMake(self.viewNum.x+self.viewNum.width, bottomY, size2.width, size2.height);
        
        
        self.line.frame = CGRectMake(0, self.viewNum.y+self.viewNum.height+10, SCREEN_WIDTH, 10);
        
        CGSize size3 = [LZCTools textSizeWithText:self.money.titleLabel.text font:kUHSystemFontWithSize(13)];
        CGFloat moneyW = 10+size3.width;
        self.money.frame = CGRectMake(SCREEN_WIDTH-10-moneyW, 0, moneyW, size3.height+8);
        self.money.centerY = self.headPic.centerY;
        
//        self.qaBtn.frame = CGRectMake(self.money.x, 0, self.money.width, self.money.height);
        self.qaBtn.frame = CGRectMake(SCREEN_WIDTH-10-size2.width, 0, size2.width, self.money.height);
        self.qaBtn.centerY = self.viewNum.centerY;
    
        self.height = self.line.y+self.line.height;
        
    }
}


- (void)imageClick:(UIImageView *)img {
    
    UITapGestureRecognizer *tapRecognizer = (UITapGestureRecognizer
                                             *)img;
    
    LZPictureBrowser *mvc = [[LZPictureBrowser alloc] init];
    [mvc showWithPictureURLs:self.mArr atIndex:[tapRecognizer.view tag]-1];
    
}

- (CGFloat)cellHeight {
    
    //    return self.sepView.y+self.sepView.height;
    return self.height;
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
